﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Emails
{
    public class EasyNetQQEmail : IEmail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="email">邮件对象</param>
        public void Send(EmailModel email)
        {
            //给邮件添加一个生成时间
            email.Body = string.Format("{0}\r\n{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), email.Body);
            //设置默认编码格式
            if (email.Encoding == null) email.Encoding = Encoding.UTF8;
            //配置邮件内容
            System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
            mailMsg.From = new MailAddress(email.FromAddress);
            mailMsg.To.Add(email.ToAddress);
            mailMsg.Subject = email.Subject;
            mailMsg.SubjectEncoding = email.Encoding; //标题编码
            mailMsg.Body = email.Body;
            mailMsg.BodyEncoding = email.Encoding;
            mailMsg.IsBodyHtml = email.IsBodyHtml;
            //配置邮件认证信息
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = "smtp.qq.com";
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(email.FromAddress, email.FromPwd);
            //发送邮件
            client.Send(mailMsg);
        }
    }
}
